package com.exercise.blog.module.main.listener;/**
 * Created by kete on 2017-05-19.
 */

import com.exercise.module.common.dao.entity.BlogUser;
import com.exercise.module.common.mq.MqListener;
import com.exercise.module.common.mq.MqService;
import com.exercise.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

/**
 * 作者：kete
 * 创建时间：2017-05-19 15:50
 * 功能描述：
 * 版本：
 */
public class MqTestListener implements ApplicationListener<ContextRefreshedEvent> {

    private Logger logger = LoggerFactory.getLogger(MqTestListener.class);

    @Autowired
    MqService mqService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (event.getApplicationContext().getParent() == null) {
            mqService.subscribe("userInfo", new MqListener() {
                @Override
                public void onMessage(Message message) {
                    try {
                        TextMessage tm = (TextMessage) message;
                        String msg = tm.getText();

                        BlogUser user = JsonUtils.toBean(msg,BlogUser.class);

                        logger.info(user.toString());


                    } catch (JMSException e) {
                        e.printStackTrace();
                    }

                }
            });

        }

    }
}
